package com.chenglu.android.payment.mvp.presenter;


import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.AddressListBean;
import com.chenglu.android.payment.bean.ComfirmOrderBean;
import com.chenglu.android.payment.bean.CouponListBean;
import com.chenglu.android.payment.bean.PayOrderBean;
import com.chenglu.android.payment.bean.PayTypeConfig;
import com.chenglu.android.payment.bean.PostDayBean;
import com.chenglu.android.payment.bean.PostTimeListBean;
import com.chenglu.android.payment.bean.TenantIdPayMethod;
import com.chenglu.android.payment.mvp.contract.ComfirmOrderContract;
import com.chenglu.android.payment.mvp.model.ComfirmOrderModel;


import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class ComfirmOrderPresenter extends BasePresenter<ComfirmOrderContract.View> implements ComfirmOrderContract.ComfirmOrderPresenter {

     private  ComfirmOrderContract.ComfirmOrderModel model;

    public ComfirmOrderPresenter(){
        model = new ComfirmOrderModel();
    }



    //需要模型分开
    @Override
    public void getComfirmOrder(String token, RequestBody requestBody,int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }
        switch (shopsTypeId){
            case Constant.typeIdShop:

                setPreComfrim(model.getComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdRestaurant:
                setPreComfrim(model.getRestaurantComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdBeauty:

                setPreComfrim(model.getBeautyComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdEducation:

                setPreComfrim(model.getEducationComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdMinimart:

                setPreComfrim(model.getMinimartComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdPharmaceuticals:

                setPreComfrim(model.getPharmaceuticalsComfirmOrder(token, requestBody));
                break;
            case Constant.typeIdBreed:

                setPreComfrim(model.getBreedComfirmOrder(token, requestBody));
                break;


        }


    }

    //预生成订单
    private void setPreComfrim(Observable<ComfirmOrderBean> comfirmOrder) {
        comfirmOrder.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ComfirmOrderBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull ComfirmOrderBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getComfirmOrder(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.onEmpty(bean.getMsg());
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();

                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }





    @Override
    public void getCouponList(String token, RequestBody requestBody,int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }
        switch (shopsTypeId){
            case Constant.typeIdShop:

                setCouponList(model.getCouponList(token, requestBody));
                break;
            case Constant.typeIdRestaurant:
                setCouponList(model.getRestaurantCouponList(token, requestBody));
                break;
            case Constant.typeIdBeauty:

                setCouponList(model.getBeautyCouponList(token, requestBody));
                break;
            case Constant.typeIdEducation:

                setCouponList(model.getEducationCouponList(token, requestBody));
                break;
            case Constant.typeIdMinimart:

                setCouponList(model.getMinimartCouponList(token, requestBody));
                break;
            case Constant.typeIdPharmaceuticals:

                setCouponList(model.getPharmaceuticalsCouponList(token, requestBody));
                break;
            case Constant.typeIdBreed:

                setCouponList(model.getBreedCouponList(token, requestBody));
                break;


        }

    }


    //预生成订单
    private void setCouponList(Observable<CouponListBean> couponList) {
        couponList.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CouponListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CouponListBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getCouponList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.onEmpty(bean.getMsg());
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();

                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }
    @Override
    public void getPayTypeConfig(String shopTypeId) {
        if(!isViewAttached()){
            return;
        }

        model.getPayTypeConfig(shopTypeId)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PayTypeConfig>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PayTypeConfig bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getPayTypeConfig(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getenantIdPayMethod(String token, String payClient, String tenantId) {
        if(!isViewAttached()){
            return;
        }

        model.getenantIdPayMethod(token,payClient,tenantId)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<TenantIdPayMethod>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull TenantIdPayMethod bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getenantIdPayMethod(bean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getGenerateOrder(String token, RequestBody requestBody,int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }
        switch (shopsTypeId){
            case Constant.typeIdShop:

                setGenerateOrder(model.getGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdRestaurant:
                setGenerateOrder(model.getRestaurantGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdBeauty:

                setGenerateOrder(model.getBeautyGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdEducation:

                setGenerateOrder(model.getEducationGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdMinimart:

                setGenerateOrder(model.getMinimartGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdPharmaceuticals:

                setGenerateOrder(model.getPharmaceuticalsGenerateOrder(token, requestBody));
                break;
            case Constant.typeIdBreed:

                setGenerateOrder(model.getBreedGenerateOrder(token, requestBody));
                break;


        }


    }


    //预生成订单
    private void setGenerateOrder(Observable<ComfirmOrderBean> generateOrder) {
        generateOrder.compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ComfirmOrderBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull ComfirmOrderBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getGenerateOrder(bean);
                                break;
                            case Constant.HTTPCODE400:
                                mView.onEmpty(bean.getMsg());
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();

                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }
    @Override
    public void getPayOrder(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }

        model.getPayOrder(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PayOrderBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PayOrderBean bean) {
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:
                                mView.getPayOrder(bean);
                                break;
                            default:
                                mView.getPayOrder(bean);
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.v(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }


    @Override
    public void getAddressList(String token) {

        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getAddressList(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<AddressListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull AddressListBean bean) {
                        LogUtils.d("ddd----");
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:

                                mView.getAddressList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v(e.getMessage());
                        NetErrorException.apiError(e,mView);
                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }



    @Override
    public void getPostDayList(int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getPostDayList()
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PostDayBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
//                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PostDayBean bean) {
                        LogUtils.d("ddd----");
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:

                                mView.getPostDayList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v(e.getMessage());
                        NetErrorException.apiError(e,mView);
//                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
//                        mView.dismissLoading();

                    }
                });
    }

    @Override
    public void getPostTimeList(int type, int shopsTypeId) {
        if(!isViewAttached()){
            return;
        }

        /*RX请求*/
        model.getPostTimeList(type)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<PostTimeListBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
//                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull PostTimeListBean bean) {
                        LogUtils.d("ddd----");
                        //每次进来都会刷新token
                        switch (bean.getCode()) {
                            case Constant.HTTPCODE200:

                                mView.getPostTimeList(bean);
                                break;
                            case Constant.HTTPCODE400:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE401:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE404:
                                ToastUtils.show(bean.getMsg());
                                break;
                            case Constant.HTTPCODE500:
                                ToastUtils.show(bean.getMsg());
                                break;
                        }


                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.e(e.getMessage());
                        NetErrorException.apiError(e,mView);
//                        mView.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
//                        mView.dismissLoading();

                    }
                });
    }
}
